<?php


namespace App\Controllers\Admin;
use Pub;

use Service,Mount,Work,Rate,User,Area;
use Input, Notification, Redirect, Sentry, Str, View;

use App\Services\Validators\PageValidator;
class ServicesController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /admin/services
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $user = Sentry::getUser();
        $METHOD = 'services';
        $ACTION = 'index';

        if($user->hasAccess('admin')){
            $services = Service::where('city_id',9)->where('section_id',0)->orderBy('created_at', 'DESC')->paginate(20);
            $works = Work::where('city',9)->orderBy('section_id', 'DESC')->get();
        }elseif($user->hasAccess('city')){
            if($user->city == 9){
                $user->city = 0;
            }
            $services = Service::where('city_id',$user->city)->where('section_id',0)->orderBy('created_at', 'DESC')->paginate(20);
            $works = Work::where('city',$user->city)->orderBy('section_id', 'DESC')->get();
        }elseif($user->hasAccess('section')){
            $services = Service::where('section_id',$user->area)->orderBy('created_at', 'DESC')->paginate(20);
            $works = Work::where('section_id',$user->area)->orderBy('created_at', 'DESC')->get();
        }
        return View::make('admin.services.index',compact('METHOD','ACTION','services','works','user'));
	}


	/**
	 * Show the form for creating a new resource.
	 * GET /admin/services/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /admin/services
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 * GET /admin/services/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /admin/services/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /admin/services/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /admin/services/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}


    public function deletemount(){
        $id = Input::get('delete_id');
        $mount = Mount::find($id);
        $mount->delete();
        $service = Service::where('mount_id',$id)->first();
        $service->delete();

        return Redirect::route('admin.services.mounts');
    }

    public function mounts(){
        $state = Input::get('state');
        $type = Input::get('type');
        $user = Sentry::getUser();


        //消除试用提醒
        if($user->shiyong_notice == 1){
            $suser = User::find($user->id);
            $suser->shiyong_notice  = 0;
            $suser->save();
        }


        $METHOD = 'services';
        $ACTION = 'mounts';


        $xiaoqus = Area::where('city_id',$user->city)->where('name','<>','')->where('level',4)->get();


        $sections = User::where('city',$user->city)->where('permissions','{"section":1}')->get();



        if($user->hasAccess('admin')){
            if(isset($state)){
                $mounts = Mount::where('state',$state)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                if($type){
                }else{

                }
                $mounts = Mount::orderBy('created_at', 'DESC')->paginate(20);
            }
        }elseif($user->hasAccess('city')){
            if(isset($state)){
                $mounts = Mount::where('city',$user->city)->where('state',$state)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                if($type == 'no'){
                    $mounts = Mount::whereHas('area', function ($query) {
                        $query->where('father_id', 0);
                    })
                        ->where('city',$user->city)
                        ->orderBy('created_at', 'DESC')
                        ->paginate(20);
                }else{
                    $mounts = Mount::where('city',$user->city)->orderBy('created_at', 'DESC')->paginate(20);
                }
            }
        }else{
            if(isset($state)){
                $mounts = Mount::where('section_id',$user->area)->where('state',$state)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $mounts = Mount::where('section_id',$user->area)->orderBy('created_at', 'DESC')->paginate(20);
            }
        }
        return View::make('admin.services.mounts',compact('METHOD','ACTION','mounts','user','sections','xiaoqus'));
    }


    public function shaixuan_mounts(){
        $from = Input::get('from');
        $user = Sentry::getUser();
        $METHOD = 'services';
        $ACTION = 'mounts';


        $xiaoqus = Area::where('city_id',$user->city)->where('name','<>','')->where('level',4)->get();


        $sections = User::where('city',$user->city)->where('permissions','{"section":1}')->get();



        if($user->hasAccess('admin')){
                $mounts = Mount::where('from',$from)->orderBy('created_at', 'DESC')->paginate(20);

        }elseif($user->hasAccess('city')){
                $mounts = Mount::where('city',$user->city)->where('from',$from)->orderBy('created_at', 'DESC')->paginate(20);

        }else{
                $mounts = Mount::where('section_id',$user->area)->where('from',$from)->orderBy('created_at', 'DESC')->paginate(20);

        }
        return View::make('admin.services.shaixuan_mounts',compact('METHOD','ACTION','mounts','user','sections','xiaoqus','from'));
    }


    public function mounts_export(){
        $state = Input::get('state');
        $user = Sentry::getUser();
        $METHOD = 'services';
        $ACTION = 'mounts';

        if($user->hasAccess('admin')){
            if($state == 'all'){
                $mounts = Mount::orderBy('created_at', 'DESC')->get();
            }else{
                $mounts = Mount::where('state',$state)->orderBy('created_at', 'DESC')->get();
            }
        }elseif($user->hasAccess('city')) {
            if($state == 'all'){
                $mounts = Mount::where('city',$user->city)->orderBy('created_at', 'DESC')->get();
            }else{
                $mounts = Mount::where('city',$user->city)->where('state',$state)->orderBy('created_at', 'DESC')->get();
            }
        }else{
            if($state == 'all'){
                $mounts = Mount::where('section_id',$user->area)->orderBy('created_at', 'DESC')->get();
            }else{
                $mounts = Mount::where('section_id',$user->area)->where('state',$state)->orderBy('created_at', 'DESC')->get();
            }
        }
        $this->_excel($mounts);

    }


    protected function _excel($lists){

        require app_path().'/lib/Excel/PHPExcel.php';
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator('http://www.jb51.net')
            ->setLastModifiedBy('http://www.jb51.net')
            ->setTitle('Office 2007 XLSX Document')
            ->setSubject('Office 2007 XLSX Document')
            ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
            ->setKeywords('office 2007 openxml php')
            ->setCategory('Result file');
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','城市')
            ->setCellValue('B1','直营商')
            ->setCellValue('C1','姓名')
            ->setCellValue('D1','电话')
            ->setCellValue('E1','联系方式')
            ->setCellValue('F1','时间');

//        var_dump($list);
        $i = 2;
        foreach($lists as $list){
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValueExplicit('A'.$i,$list->cityname->name,\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('B'.$i,$list->section->name,\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('C'.$i,$list->mount_name)
                ->setCellValueExplicit('D'.$i,$list->mount_mobile,\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue('E'.$i,$list->xiaoqu->name.'-'.$list->address)
                ->setCellValueExplicit('F'.$i,$list->created_at,\PHPExcel_Cell_DataType::TYPE_STRING);
            $i++;
        }

        $objPHPExcel->getActiveSheet()->setTitle('Sheet1');
        $objPHPExcel->setActiveSheetIndex(0);
        $filename='客户资料';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');


        $objWriter->save('php://output');

    }




    public function shift(){
        $state = Input::get('state');
        $user = Sentry::getUser();
        $METHOD = 'services';
        $ACTION = 'shift';

        if($user->hasAccess('section')){
            if(isset($state)){
                $mounts = Mount::where('shift_area',$user->area)->where('state',$state)->where('shift',1)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $mounts = Mount::where('shift_area',$user->area)->where('shift',1)->orderBy('created_at', 'DESC')->paginate(20);
            }
        }
        return View::make('admin.services.shift',compact('METHOD','ACTION','mounts','user'));
    }
    public function shiftt(){
        $state = Input::get('state');
        $user = Sentry::getUser();
        $METHOD = 'services';
        $ACTION = 'shiftt';

        if($user->hasAccess('section')){
            if(isset($state)){
                $mounts = Mount::where('section_id',$user->area)->where('state',$state)->where('shift',1)->orderBy('created_at', 'DESC')->paginate(20);
            }else{
                $mounts = Mount::where('section_id',$user->area)->where('shift',1)->orderBy('created_at', 'DESC')->paginate(20);
            }
        }
        return View::make('admin.services.shift',compact('METHOD','ACTION','mounts','user'));
    }

    public function paidan(){
        $type = Input::get('paidan_type');
        if($type == 're'){//为重新派单
            $rate = Rate::find(Input::get('service_id'));
            if($rate->service_id == 0){
                $mount = Service::find($rate->mount_id);
                $service_id = Service::insert_try($mount->customer_id, $mount->id, 12, 2, $mount->section_id, '',$mount->city,Input::get('radio'));
                $new_service = Service::find($service_id);
            }else{
                $service = Service::find($rate->service_id);
                $service_id = Service::insert_try($service->customer_id, $service->mount_id, 12, 2, $service->section_id, '',$service->city_id,Input::get('radio'));
                $new_service = Service::find($service_id);
            }
            $work = Work::find(Input::get('radio'));

            if($work->openid != ''){
                $this->forworker($new_service,$work);
            }

            $this->forcustomer($new_service,$work);
        }else{
            Service::paidan(Input::get('service_id'),Input::get('radio'));
            $work = Work::find(Input::get('radio'));
            $service = Service::find(Input::get('service_id'));

            if($work->openid != ''){
                $this->forworker($service,$work);
            }
            $this->forcustomer($service,$work);
        }

        return Redirect::route('admin.services.index');
    }

    public function forworker($service,$work){

        $data[0] = '尊敬的'.$work->name.'您有一条新的工单需要处理';

        $type[1] = '试用';
        $type[10] = '订购';
        $type[11] = '拆机';
        $type[12] = '维修';
        $type[13] = '换芯';


        $data[1] = $type[$service->type];
        $data[2] = $service->mount->machine->name;
        $data[3]= $work->name;
        $data[4]= '网点';
        $data[5]= $work->mobile;
        $data[6]= '点击查看工单详细信息';
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc64f995bebe39161&redirect_uri=http%3a%2f%2fcrm.leaderphos.com%2fdiscern&response_type=code&scope=snsapi_userinfo&state=service_'.$service->id.'#wechat_redirect';
        \Template::newone($work->openid,$data,$url,1);
    }
    public function forcustomer($service,$work){

        $data[0] = '尊敬的客户您有一条工单已派单';

        $type[1] = '试用';
        $type[10] = '订购';
        $type[11] = '拆机';
        $type[12] = '维修';
        $type[13] = '换芯';


        $data[1] = $type[$service->type];
        $data[2] = $service->mount->machine->name;
        $data[3]= $work->name;
        $data[4]= '网点';
        $data[5]= $work->mobile;
        $data[6]= '点击查看详细信息';
        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc64f995bebe39161&redirect_uri=http%3a%2f%2fcrm.leaderphos.com%2fdiscern&response_type=code&scope=snsapi_userinfo&state=service#wechat_redirect';
        \Template::newone($service->customer->openid,$data,$url,1);
    }

}